package com.jin.ks;

import android.app.Application;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.widget.Toast;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;


/**
 * Created by Administrator on 2018/2/8.
 */
public class XposedHook implements IXposedHookLoadPackage {
    String name = "";
    String follow = "";
    String kuaiId = "";
    String fan = "";
    String moment = "";
    String like = "";
    String photo = "";
    String text = "";
    String birthday = "";
    String todayFollow ="";
    String time = "";
    Uri uri_user = Uri.parse("content://com.jin.ks/UserInfo");
    boolean isMe = false;

    @Override
    public void handleLoadPackage(final XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {

        if (!lpparam.packageName.equals("com.smile.gifmaker")) {
            return;
        }

        XposedBridge.log("HOOKuser");

        XposedHelpers.findAndHookMethod(Application.class, "attach", Context.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                final Context context = (Context) param.args[0];
                ClassLoader classLoader = ((Context) param.args[0]).getClassLoader();
                final Class<?> C = XposedHelpers.findClass("com.google.gson.stream.a", classLoader);
                XposedHelpers.findAndHookMethod("com.kuaishou.android.model.user.d", classLoader, "a", C, new XC_MethodHook() {
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        super.beforeHookedMethod(param);

                    }
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        super.afterHookedMethod(param);
                        ContentResolver resolver = context.getContentResolver();
                        Cursor cursor = resolver.query(uri_user, new String[]{"id", "mStatus", "mName", "mKuaiId", "mFan", "mFollow", "mMoment", "mLike", "mPhoto", "mText", "mBirthday", "mTodayFollow", "mTime"}, null, null, null);
                        while (cursor.moveToNext()) {

                            if (cursor.getString(1).equals("1")) {
                                name = cursor.getString(2);
                                kuaiId = cursor.getString(3);
                                fan = cursor.getString(4);
                                follow = cursor.getString(5);
                                moment = cursor.getString(6);
                                like = cursor.getString(7);
                                photo = cursor.getString(8);
                                text = cursor.getString(9);
                                birthday = cursor.getString(10);
                                todayFollow = cursor.getString(11);
                                time = cursor.getString(12);
                                break;
                            }
                            // 将表中数据取出
                        }
                        cursor.close();
                        Object UserProfile = param.getResult();
                        if (UserProfile.toString().indexOf("UserProfile") != -1) {
                            Object mOwnerCount = UserProfile.getClass().getField("mOwnerCount").get(UserProfile);
                            Object mProfile = UserProfile.getClass().getField("mProfile").get(UserProfile);
                            //XposedBridge.log("**********HOOK:com.yxcorp.retrofit.model************\n"+mOwnerCount.getClass().getField("mFollow").get(mOwnerCount).toString());
                            //res.getClass().getField("mUserProfile").get(res).getClass().getField("mCityName").set(res.getClass().getField("mUserProfile").get(res),"银河系 冥王星");
                            //mUserProfile.getClass().getField("mCityName").set(mUserProfile,"银河系 海王星");//修改城市
                            //mOwnerCount.getClass().getField("mFollow").set(mOwnerCount,678);//修改关注
                            if (!getValue(mProfile, "mName").equals("昵称被吃了6")){
                                isMe = false;
                                return;
                            }


                            isMe = true;
                            setValue(mOwnerCount, "mFollow", Integer.parseInt(follow));//修改关注
                            setValue(mOwnerCount, "mFan", Integer.parseInt(fan));//修改粉丝
                            //setValue(mOwnerCount, "mMoment", Integer.parseInt(moment));//修改说说
                            //setValue(mOwnerCount, "mLike", Integer.parseInt(like));//修改赞
                            //setValue(mOwnerCount, "mPublicPhoto", Integer.parseInt(photo));//修改作品
                            //setValue(mOwnerCount, "mPhoto", Integer.parseInt(photo));
                            setValue(mProfile, "mName", name);//修改昵称
                            setValue(mProfile, "mKwaiId", kuaiId);//修改快手号
                            setValue(mProfile, "mText", text);//修改签名
                            setValue(UserProfile, "mBirthday", birthday);//修改生日  mConstellation星座
                            //XposedBridge.log("&&&&&&&" + getValue(UserProfile, "mBirthday") + "&&&&&&&");
                            //XposedBridge.log("&&&&&&&" + getValue(UserProfile, "mConstellation") + "&&&&&&&");

                        }
                    }
                });
                //final Class<?> c2 = XposedHelpers.findClass("com.google.gson.i", classLoader);
                XposedHelpers.findAndHookMethod("com.yxcorp.gifshow.profile.presenter.profile.ProfileTabPresenter", classLoader, "b", int.class,CharSequence.class,new XC_MethodHook() {
                    //hook 作品tab
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        super.beforeHookedMethod(param);
                        //param.args[0] = 666;
                        if (!isMe)
                            return;

                        switch (param.args[1].toString()){
                            case "作品":
                                param.args[0] = Integer.parseInt(photo);
                                break;
                            case "说说":
                                param.args[0] = Integer.parseInt(moment);
                                break;
                            case "私密":
                                break;
                            case "赞":
                                param.args[0] = Integer.parseInt(like);
                                break;
                        }
                    }

                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        super.afterHookedMethod(param);
                    }
                });

                //Hook 关注
                XposedHelpers.findAndHookMethod("com.yxcorp.gifshow.retrofit.f", classLoader, "a", boolean.class, String.class, String.class, String.class, String.class, String.class, String.class, String.class, int.class, new XC_MethodHook() {
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        super.beforeHookedMethod(param);
                        param.args[1] = "1022033160";
                        todayFollow = ""+(Integer.parseInt(todayFollow)+1);
                        follow = ""+(Integer.parseInt(follow)+1);
                        ContentValues vs = new ContentValues();
                        vs.put("mTodayFollow",todayFollow);
                        vs.put("mFollow",follow);
                        context.getContentResolver().update(uri_user,vs,"mName=?",new String[]{name});
                    }

                    protected  void afterHookedMethod(MethodHookParam param) throws Throwable{
                        super.afterHookedMethod(param);
                        XposedBridge.log(param.args[1].toString());
                    }
                });
            }
        });
    }

    public String getValue(Object a, String b) throws Throwable {
        String c = a.getClass().getField(b).get(a).toString();
        return c;
    }

    public void setValue(Object a, String b, Object value) throws Throwable {
        a.getClass().getField(b).set(a, value);
    }
}
